#include "apue.h"
#include <sys/wait.h>

int main() {
    pid_t pid;
    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid == 0) { /* first child */
        if ((pid = fork()) < 0) {
            err_sys("fork error");
        } else if (pid > 0) {
            exit(0); /* parent from second fork == first child */
        }
        sleep(2);
        printf("second child, paren t pid = %ld\n", (long)getpid());
        exit(0);
    }
    if (waitpid(pid, NULL, 0) != pid) { /* wait for the first child */
        err_sys("waitpid error");
    }
    exit(0);
}